package cn.itrip.service;

import cn.itrip.beans.vo.hotel.SearchHotelVO;
import cn.itrip.common.EmptyUtils;
import cn.itrip.common.Page;
import cn.itrip.dao.BaseQuery;
import cn.itrip.vo.ItripHotelVo;
import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.stereotype.Service;

/**
 * @author jie
 * @date 2019/5/7 -11:12
 */
@Service
public class HotelServiceImpl implements HotelService {

    private BaseQuery baseQuery = new BaseQuery("http://localhost:8080/solr/hotel/");

    @Override
    public Page<ItripHotelVo> queryPage(SearchHotelVO searchHotelVO, Integer pageNo, Integer pageSize) throws Exception {
        SolrQuery query = new SolrQuery("*:*");
        StringBuffer sb = new StringBuffer();
        if (EmptyUtils.isNotEmpty(searchHotelVO)){
            if(EmptyUtils.isNotEmpty(searchHotelVO.getDestination())){
                sb.append("destination:"+searchHotelVO.getDestination());
            }
            if(EmptyUtils.isNotEmpty(searchHotelVO.getDescSort())){
                query.addSort(searchHotelVO.getDescSort(),SolrQuery.ORDER.desc);
            }
        }

        query.setQuery(sb.toString());
        Page page = baseQuery.queryPage(query, pageNo, pageSize, ItripHotelVo.class);
        return page;
    }

    @Override
    public Page<ItripHotelVo> queryByCityId(int cityId, Integer pageNo, Integer pageSize) throws Exception {
        SolrQuery query = new SolrQuery("*:*");
        query.setQuery("cityId:"+cityId);
        Page page = baseQuery.queryPage(query, pageNo, pageSize, ItripHotelVo.class);
        return page;
    }


}
